package org.rufolfheszele.andpixelbyandpixel;

import java.util.List;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;

/**
 * This class draws a static picture to the window
 *
 * @see IDrawController
 * @author Rudolf Heszele heszele@gmail.com
 * @version 1.0
 * @since 2012-03-08
 */
public class StaticDrawController implements IDrawController
{
		/**
		 * The only constructor takes the bitmap to be painted
		 *
		 * @param bitmap The bitmap to be painted
		 * @param refresh if it is true, the wxWidgets will be forced to do a refresh
		 */
		public StaticDrawController(Bitmap bitmap, boolean refresh)
		{
			mBitmap = bitmap;
			if(true == refresh)
				this.refreshImage(true, null);
		}

		// Methods inherited from Controller

		/**
		 * @see IController::handleMouseDown
		 */
		public boolean handleTouchDown(Point point)
		{
			//Nothing to do
			return false;
		}
		
		/**
		 * @see IController::handleMouseUp
		 */
		public boolean handleTouchUp(Point point)
		{
			//Nothing to do
			return false;
		}
		
		/**
		 * @see IController::handleMouseMotion
		 */
		public boolean handleTouchMove(Point point)
		{
			// Nothing to do
			return false;
		}
		
		/**
		 * @see IController::getBitmapToPaint
		 */
		public Bitmap getBitmap()
		{
			return mBitmap;
		}

		// Methods inherited from IDrawController

		/**
		 * @see IDrawController::getActualVehicle
		 */
		public Vehicle getActualVehicle()
		{
			// Nothing to do
			return null;
		}
		
		/**
		 * @see IDrawController::getRemoteVehicle
		 */
		public Vehicle getRemoteVehicle()
		{
			// Nothing to do
			return null;
		}
		
		/**
		 * @see IDrawController::getActualVehicleArrows
		 */
		public List<Point> getActualVehicleArrows()
		{
			// Nothing to do
			return null;
		}
		
		/**
		 * @see IDrawController::getRemoteVehicleArrows
		 */
		public List<Point> getRemoteVehicleArrows()
		{
			// Nothing to do
			return null;
		}
		
		/**
		 * This method paints the dots to the picture, and optionally makes a refresh
		 *
		 * @param doRefresh If true wxWidgets will be forced to do a refresh
		 * @param mainPanel The MainPanel to do the refresh on
		 */
		public void refreshImage(boolean doRefresh, MainImageView mainImageView)
		{
			// Get the MainPanel to be able to force a refresh
			if(null == mainImageView)
				mainImageView = MainApplication.getMainApplication().getMainActivity().getMainImageView();
			this.paintDots();
			if(true == doRefresh)
				mainImageView.invalidate();
		}

		// Own methods

		public void setBitmap(Bitmap bitmap)
		{
			mBitmap = bitmap;
		}

		/**
		 * Paints the dots to the bitmap
		 */
		protected void paintDots()
		{
			Canvas canvas = new Canvas(mBitmap);
			Paint paint = this.getPaint(Color.RED, 1);
			int dotSize = MainApplication.getMainApplication().getConfiguration().getDotSize();
			
			for(int i = 0; i < mBitmap.getWidth(); i += dotSize)
			{
				for(int j = 0; j < mBitmap.getHeight(); j += dotSize)
					canvas.drawPoint(i, j, paint);
			}
		}
		
		/**
		 * Sets the pen with the given color and width
		 *
		 * @dc The device context to set the pen in
		 * @color The requested color of the pen
		 * @width The requested width of the pen
		 */
		protected Paint getPaint(int color, int width)
		{
			Paint paint = new Paint();
			
			paint.setColor(color);
			paint.setStrokeWidth(width);
			
			return paint;
		}

		private Bitmap mBitmap = null;
};
